package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.system.domain.SysOperLog;
import com.muyu.system.domain.model.SysOperLogAddModel;
import com.muyu.system.domain.model.SysOperlogPageQueryModel;
import com.muyu.system.domain.resp.SysOperlogListResp;
import com.muyu.system.mapper.SysOperLogMapper;
import com.muyu.system.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 操作日志 服务层处理
 *
 * @author muyu
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {
    private final SysOperLogMapper operLogMapper;

    @Autowired
    public SysOperLogServiceImpl(SysOperLogMapper operLogMapper) {
        this.operLogMapper = operLogMapper;
    }

    /**
     * 新增操作日志
     *
     * @param sysOperLogAddModel 操作日志记录添加模型
     */
    @Override
    public void insertOperlog (SysOperLogAddModel sysOperLogAddModel) {
        this.save(
                SysOperLog.builder()
                        .operId(sysOperLogAddModel.getOperId())
                        .title(sysOperLogAddModel.getTitle())
                        .businessType(sysOperLogAddModel.getBusinessType())
                        .method(sysOperLogAddModel.getMethod())
                        .requestMethod(sysOperLogAddModel.getRequestMethod())
                        .operatorType(sysOperLogAddModel.getOperatorType())
                        .operName(sysOperLogAddModel.getOperName())
                        .deptName(sysOperLogAddModel.getDeptName())
                        .operUrl(sysOperLogAddModel.getOperUrl())
                        .operIp(sysOperLogAddModel.getOperIp())
                        .operParam(sysOperLogAddModel.getOperParam())
                        .jsonResult(sysOperLogAddModel.getJsonResult())
                        .status(sysOperLogAddModel.getStatus())
                        .errorMsg(sysOperLogAddModel.getErrorMsg())
                        .operTime(sysOperLogAddModel.getOperTime())
                        .costTime(sysOperLogAddModel.getCostTime())
                        .build()
        );
    }


    /**
     * 操作日志 分页查询
     * @param sysOperlogPageQueryModel 分页模型查询
     * @return 分页结果
     */
    @Override
    public PageQueryModel<SysOperlogListResp> pageQuery(SysOperlogPageQueryModel sysOperlogPageQueryModel) {
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(sysOperlogPageQueryModel.getOperIp()),SysOperLog::getOperIp, sysOperlogPageQueryModel.getOperIp());
        queryWrapper.like(StringUtils.isNotEmpty(sysOperlogPageQueryModel.getTitle()),SysOperLog::getTitle, sysOperlogPageQueryModel.getTitle());
        queryWrapper.like(StringUtils.isNotEmpty(sysOperlogPageQueryModel.getOperName()),SysOperLog::getOperName, sysOperlogPageQueryModel.getOperName());
        queryWrapper.eq(Objects.nonNull(sysOperlogPageQueryModel.getBusinessType()),SysOperLog::getBusinessType, sysOperlogPageQueryModel.getBusinessType());
        queryWrapper.eq(Objects.nonNull(sysOperlogPageQueryModel.getStatus()),SysOperLog::getStatus, sysOperlogPageQueryModel.getStatus());
        queryWrapper.gt(Objects.nonNull(sysOperlogPageQueryModel.getBeginTime()),SysOperLog::getOperTime, sysOperlogPageQueryModel.getBeginTime());
        queryWrapper.lt(Objects.nonNull(sysOperlogPageQueryModel.getEndTime()),SysOperLog::getOperTime, sysOperlogPageQueryModel.getEndTime());
        Page<SysOperLog> page = this.page(sysOperlogPageQueryModel.buildPage(), queryWrapper);
        List<SysOperlogListResp> sysOperlogListResp = page.getRecords().stream()
                .map(SysOperlogListResp::infoBuildToResp)
                .toList();
        Page<SysOperlogListResp> respPage = new Page<>(page.getCurrent(),page.getSize());
        respPage.setRecords(sysOperlogListResp);
        respPage.setTotal(page.getTotal());
        respPage.setOrders(page.orders());
        return PageQueryModel.of(respPage);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     *
     * @return 结果
     */
    @Override
    public void deleteOperLogByIds (List<Long> operIds) {
        this.removeByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     *
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById (Long operId) {
        return getById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog () {
        operLogMapper.cleanOperLog();
    }

}
